<?php
namespace app\controller\handler;
use app\controller\Common;
use think\Db;
use think\Model;

class Permission extends Common{

  //获取项目列表
  public function getProjectList(){
    $base = input('post.');
    $page = isset($base['page']) ? trim($base['page']): '';
    $pagesize = isset($base['pagesize']) ? trim($base['pagesize']): '';
    $current = ($page-1)*$pagesize;
    $sql = "select p.*,u.nickname,u.role_id
            from `#__project` p
            left join `#__user` u on u.id=p.root
            where p.status=1 order by p.id desc limit ?,?
          ";
    $countSql = "select count(*) as total
            from `#__project` p
            where p.status=1 order by p.id desc
          ";
    $res = Db::query($sql,[$current,$pagesize]);
    $count = Db::query($countSql);
    if($res && count($res)>0){
      $result['ret'] = 1;
      $result['data'] = $res;
      $result['total'] = $count[0]['total'];
    }else{
      $result['ret'] = 0;
      $result['msg'] = '未查询到数据';
    }
    return $result;
  }

  public function getProjectDetailById(){
    $wh['id'] = $_REQUEST['id'];
    $wh['status'] = 1;
    $res = model('Project')::get($wh);
    if($res){
      $result['ret'] = 1;
      $result['data'] = $res;
    }else{
      $result['ret'] = 0;
      $result['msg'] = '未查询到数据';
    }
    return $result;
  }

  public function addProject(){
    $base = $_REQUEST['base'];
    $base['ctime'] = time();
    $res = model('Project')::create($base);
    if($res){
      $result['ret'] = 1;
      $result['msg'] = '添加成功';
    }else{
      $result['ret'] = 0;
      $result['msg'] = '添加失败';
    }
    return $result;
  }

  public function editProject(){
    $base = $_REQUEST['base'];
    $wh['id'] = $_REQUEST['id'];
    $res = model('Project')::where($wh)->update($base);
    if($res){
      $result['ret'] = 1;
      $result['msg'] = '编辑成功';
    }else{
      $result['ret'] = 0;
      $result['msg'] = '编辑失败';
    }
    return $result;
  }

  public function deleteProject(){
    $wh['id'] = $_REQUEST['id'];
    $up['status'] = 0;
    $res = model('Project')::where($wh)->update($up);
    if($res){
      $result['ret'] = 1;
      $result['msg'] = '删除成功';
    }else{
      $result['ret'] = 0;
      $result['msg'] = '删除失败';
    }
    return $result;
  }

  public function setProjectRoot(){
    $base = $_REQUEST['base'];
    $base['password'] = base_encode($base['password']);
    $base['email'] = $base['nickname'];
    if(isset($_REQUEST['root']) && $_REQUEST['root'] > 0){
      //修改超管
      $wh['id'] = $_REQUEST['root'];
      $res = model('User')::where($wh)->update($base);
      if($res){
        $result['ret'] = 1;
        $result['msg'] = '修改成功';
      }else{
        $result['ret'] = 0;
        $result['msg'] = '修改失败';
      }
    }else{
      //新增超管
      if(!isset($base['cover'])){
        $base['cover'] = '/static/img/index/ceo.jpg';
      }
      $base['type'] = 2;
      $base['ctime'] = time();
      //新增project_role超管角色
      $res3 = model('ProjectRole')::create(['project_id'=>$_REQUEST['id'],'name'=>'超级管理员','description'=>'超级管理员']);
      if(!$res3){
        $result['ret'] = 0;
        $result['msg'] = '新增项目超管角色失败';
        return $result;
      }
      $base['role_id'] = $res3['id'];
      //新增超管user账号
      $res = model('User')::create($base);
      if($res){
        $wh['id'] = $_REQUEST['id'];
        $up['root'] = $res['id'];
        //更新project超管id
        $res2 = model('Project')::where($wh)->update($up);
        if($res2){
          $result['ret'] = 1;
          $result['msg'] = '设置成功';
        }else{
          $result['ret'] = 0;
          $result['msg'] = '设置失败，请联系管理员';
        }
      }else{
        $result['ret'] = 0;
        $result['msg'] = '新增超管账号失败';
      }
    }
    return $result;
  }

  public function setProjectPower(){
    $base = $_REQUEST['base'];
    $wh['role_id'] = $base['role_id'];
    $wh['status'] = 1;
    $up['status'] = 0;
    //判断角色是否设置权限
    $res = model('ProjectRolePower')::get($wh);
    if($res){
      //将之前设置置为无效
      $res2 = model('ProjectRolePower')::where($wh)->update($up);
      if(!$res2){
        $result['ret'] = 0;
        $result['msg'] = '编辑失败';
        return $result;
      }
    }
    //新增设置权限记录
    $base['ctime'] = time();
    $res3 = model('ProjectRolePower')::create($base);
    if($res3){
      $result['ret'] = 1;
      $result['msg'] = '设置成功';
    }else{
      $result['ret'] = 0;
      $result['msg'] = '设置失败';
    }
    return $result;
  }

  public function getPowersByRoleId(){
    $wh['role_id'] = $_REQUEST['role_id'];
    $wh['status'] = 1;
    $res = model('ProjectRolePower')::get($wh);
    if($res){
      $result['ret'] = 1;
      $result['data'] = $res;
    }else{
      $result['ret'] = 0;
      $result['msg'] = '未获取到数据';
    }
    return $result;
  }

  //获取权限值列表
  public function getPowerList(){
    $base = input('post.');
    $page = isset($base['page']) ? trim($base['page']): '';
    $pagesize = isset($base['pagesize']) ? trim($base['pagesize']): '';
    $current = ($page-1)*$pagesize;
    //查询当前用户所在项目的最高权限
    $projectId = model('User')::getCurrentProjectId();
    if($projectId != 0){
      $sql1 = "select prp.power_ids
             from `#__project` p
             left join `#__user` u on u.id=p.root
             left join `#__project_role_power` prp on prp.role_id=u.role_id and prp.status=1
             where p.id=$projectId
          ";
      $res1 = Db::query($sql1);
      $maxPowers = $res1[0]['power_ids'];
      $cond = " and p.id in ($maxPowers) ";
    }else{
      $cond = '';
    }
    //end
    $sql = "select p.*
            from `#__power` p
            where p.status=1 $cond order by p.id desc limit $current,$pagesize
          ";
    $countSql = "select count(*) as total
            from `#__power` p
            where p.status=1 $cond order by p.id desc
          ";
    $res = Db::query($sql);
    $count = Db::query($countSql);
    if($res){
      $result['ret'] = 1;
      $result['data'] = $res;
      $result['total'] = $count[0]['total'];
    }else{
      $result['ret'] = 0;
      $result['msg'] = '未查询到数据';
    }
    return $result;
  }

  public function getPowerDetailById(){
    $wh['id'] = $_REQUEST['id'];
    $wh['status'] = 1;
    $res = model('Power')::get($wh);
    if($res){
      $result['ret'] = 1;
      $result['data'] = $res;
    }else{
      $result['ret'] = 0;
      $result['msg'] = '未查询到数据';
    }
    return $result;
  }

  public function addPower(){
    $base = $_REQUEST['base'];
    $res = model('Power')::create($base);
    if($res){
      $result['ret'] = 1;
      $result['msg'] = '添加成功';
    }else{
      $result['ret'] = 0;
      $result['msg'] = '添加失败';
    }
    return $result;
  }

  public function editPower(){
    $base = $_REQUEST['base'];
    $wh['id'] = $_REQUEST['id'];
    $res = model('Power')::where($wh)->update($base);
    if($res){
      $result['ret'] = 1;
      $result['msg'] = '编辑成功';
    }else{
      $result['ret'] = 0;
      $result['msg'] = '编辑失败';
    }
    return $result;
  }

  public function deletePower(){
    $wh['id'] = $_REQUEST['id'];
    $up['status'] = 0;
    $res = model('Power')::where($wh)->update($up);
    if($res){
      $result['ret'] = 1;
      $result['msg'] = '删除成功';
    }else{
      $result['ret'] = 0;
      $result['msg'] = '删除失败';
    }
    return $result;
  }

  //验证操作权限
  public function checkOperatePermission(){
    $res = model('User')::checkHandlerPermission($_POST['permission']);
    if($res){
      $result['ret'] = 1;
      $result['msg'] = 'OK';
    }else{
      $result['ret'] = 0;
      $result['msg'] = '无操作权限';
    }
    return $result;
  }


}//END